package models.general;

import play.db.ebean.Model;

import javax.persistence.Entity;
import java.util.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * User: bjornth
 * Date: 26.3.2013
 * Time: 17:23
 * To change this template use File | Settings | File Templates.
 */
@Entity
public class vNemandiAfangarOnn extends Model {

    public int persona_id;
    public String nafn;
    public int notandi_id;
    public String kennitala;
    public int skoli_id;
    public String namsgrein_afanganr;
    public int afangi_id;
    public String hopur;
    public int hopur_id;
    public String onn_eink;
    public Date dags_fra;
    public Date dags_til;
    public int onn_id;
    public int annir_afangar_id;

    public static Finder<Integer,vNemandiAfangarOnn> find = new Finder<Integer,vNemandiAfangarOnn>(
            Integer.class, vNemandiAfangarOnn.class
    );
    public static List<vNemandiAfangarOnn> getCoursesClasses (int notandi_id) {
        List<vNemandiAfangarOnn> afangarNemandi = vNemandiAfangarOnn.find.where("trunc(sysdate) between dags_fra and dags_til").where().eq("notandi_id", notandi_id).findList();
        Collections.sort(afangarNemandi, new Comparator<vNemandiAfangarOnn>() {
            @Override
            public int compare(vNemandiAfangarOnn o1, vNemandiAfangarOnn o2) {
                Integer i = o1.getAfangi_id();
                return i.compareTo(o2.getAfangi_id());
            }
        });
        return afangarNemandi;
    }

    public static List<vNemandiAfangarOnn> findStudentsByGroup (int hopur_id)   {
        List<vNemandiAfangarOnn> studentsInGroup = vNemandiAfangarOnn.find.where("trunc(sysdate) between dags_fra and dags_til").where().eq("hopur_id", hopur_id).orderBy("persona_id asc").findList();
        Collections.sort(studentsInGroup, new Comparator<vNemandiAfangarOnn>() {
            @Override
            public int compare(final vNemandiAfangarOnn o1, final vNemandiAfangarOnn o2) {
                if (o1.getNafn() == null || o2.getNafn() == null)
                    return 0;
                return (o2.getNafn().compareTo(o1.getNafn())*(-1));
            }
        });
        return studentsInGroup;

    }
    public static List<vNemandiAfangarOnn> getNemendurHop(int hopur_id){
        List<vNemandiAfangarOnn> NemandiAfangarOnn = vNemandiAfangarOnn.find.where().eq("hopur_id", hopur_id).orderBy("nafn asc").findList();
        return NemandiAfangarOnn;
    }
    public static vNemandiAfangarOnn findByStudentAndAfangiID(int notandi_id,int afangi_id){
        return vNemandiAfangarOnn.find.where().eq("notandi_id", notandi_id).where().eq("afangi_id", afangi_id).findUnique();
    }
    public static List<String> findByStudentID(int notandi_id){
        vNemandiAfangarOnn nem = vNemandiAfangarOnn.find.where().eq("notandi_id", notandi_id).setMaxRows(1).findUnique();
        List<String> nafnKt = new ArrayList<String>();
        nafnKt.add(nem.getNafn());
        nafnKt.add(nem.getKennitala());
        return nafnKt;
    }

    public int getPersona_id() {
        return persona_id;
    }


    public void setPersona_id(int persona_id) {
        this.persona_id = persona_id;
    }

    public String getNafn() {
        return nafn;
    }

    public void setNafn(String nafn) {
        this.nafn = nafn;
    }

    public int getNotandi_id() {
        return notandi_id;
    }

    public void setNotandi_id(int notandi_id) {
        this.notandi_id = notandi_id;
    }

    public String getKennitala() {
        return kennitala;
    }

    public void setKennitala(String kennitala) {
        this.kennitala = kennitala;
    }

    public int getSkoli_id() {
        return skoli_id;
    }

    public void setSkoli_id(int skoli_id) {
        this.skoli_id = skoli_id;
    }

    public String getNamsgrein_afanganr() {
        return namsgrein_afanganr;
    }

    public void setNamsgrein_afanganr(String namsgrein_afanganr) {
        this.namsgrein_afanganr = namsgrein_afanganr;
    }

    public int getAfangi_id() {
        return afangi_id;
    }

    public void setAfangi_id(int afangi_id) {
        this.afangi_id = afangi_id;
    }

    public String getHopur() {
        return hopur;
    }

    public void setHopur(String hopur) {
        this.hopur = hopur;
    }

    public int getHopur_id() {
        return hopur_id;
    }

    public void setHopur_id(int hopur_id) {
        this.hopur_id = hopur_id;
    }

    public String getOnn_eink() {
        return onn_eink;
    }

    public void setOnn_eink(String onn_eink) {
        this.onn_eink = onn_eink;
    }

    public Date getDags_fra() {
        return dags_fra;
    }

    public void setDags_fra(Date dags_fra) {
        this.dags_fra = dags_fra;
    }

    public Date getDags_til() {
        return dags_til;
    }

    public void setDags_til(Date dags_til) {
        this.dags_til = dags_til;
    }

    public int getOnn_id() {
        return onn_id;
    }

    public void setOnn_id(int onn_id) {
        this.onn_id = onn_id;
    }

    public int getAnnir_afangar_id() {
        return annir_afangar_id;
    }

    public void setAnnir_afangar_id(int annir_afangar_id) {
        this.annir_afangar_id = annir_afangar_id;
    }
}
